package com.homson.servicedomain.domain.account;

import com.homson.coreservice.servicecommon.enumeration.AccountTypeEnum;
import com.homson.coreservice.serviceinfrastructure.converter.AccountConverter;
import com.homson.coreservice.serviceinfrastructure.datasource.model.account.AccountDO;
import com.homson.coreservice.serviceinfrastructure.datasource.repository.account.AccountRepository;
import com.homson.coreservice.serviceinfrastructure.dto.AccountDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author: homson_leo
 * @contact: hey_超级巨星（Homson_Leo）- 刘泓岑 1196816884@qq.com
 * @time: 2025/10/22 11:13 PM
 **/
@Service
public class AccountQueryCommand {
    
    @Autowired
    private AccountRepository accountRepository;
    
    public Boolean isAccountExist(Long accountId, AccountTypeEnum accountType) {
        AccountDO accountDO = accountRepository.findByAccountIdAndType(accountId, accountType.name());
        return Objects.nonNull(accountDO);
    }
    
    public List<Long> queryAllAccountIdList(Long userId) {
        List<AccountDO> accountDOs = accountRepository.findAllByUserId(userId);
        return accountDOs.stream().map(AccountDO::getAccountId).toList();
    }
    
    public List<AccountDto> queryAllAccountList(Long userId, Integer pageNum, Integer pageSize){
        // TODO 健壮
        List<AccountDO> allAccountDOs = accountRepository.findAllAccountByPage(userId, pageNum, pageSize);
        if(CollectionUtils.isEmpty(allAccountDOs)){
            return Collections.emptyList();
        }
        List<AccountDto> accountDtos = AccountConverter.convert(allAccountDOs);
        return accountDtos;
    }
    
    public List<AccountDto> findAccountByAccountIds(List<Long> accountIds) {
        if (CollectionUtils.isEmpty(accountIds)) {
            return Collections.emptyList();
        }
        List<AccountDO> accountDOS = accountRepository.findByAccountIds(accountIds);
        if (CollectionUtils.isEmpty(accountDOS)) {
            return Collections.emptyList();
        }
        List<AccountDto> accountDtoList = AccountConverter.convert(accountDOS);
        return accountDtoList;
    }
    
    
   
   
}
